<?php namespace Admin\Controller; 
use Hdphp\Controller\Controller;
//登录
class LoginController extends Controller{
    //显示登录页面
    public function index(){
   		if(IS_POST){
   			//判断验证码是否正确
   			$code=Q('post.code','','strtoupper');
			if($_SESSION['code']!=$code) View::error("验证码输入错误");
   			//判断用户名是否存在
   			$username=Q('post.username');
			$data = Db::table('admin')->where("ausername='{$username}'")->first();
			if(!$data) View::error("用户名或密码错误");
			//判断密码是否正确
			$password=Q('post.password','','md5');
			if($data['apassword']!=$password) View::error("用户名或密码错误");
			//登录成功
			//将上次登录时间、IP存入session
			$_SESSION['info']=[
				'aid'=>$data['aid'],
				'anickname'=>$data['anickname'],
				'aip'=>$data['aip'],
				'atime'=>$data['atime']
			];
			//更新本次登录时间、IP
			$atime=time();
			$aip=Request::ip();
//			$aip=$_SERVER['SERVER_ADDR'];
			Db::table('admin')->where("ausername='{$username}'")->update(['atime'=>$atime]);
			Db::table('admin')->where("ausername='{$username}'")->update(['aip'=>$aip]);
			View::success("登录成功",U('Index/index'));
   		}
       	View::make();
    }
	//显示验证码
	public function code(){
		Code::num(1)->make();
	}
	//退出
	public function out(){
		session_unset();
		session_destroy();
		go(U('index'));
	}
}
